#include "imagehandler.h"
#include <QFileInfo>

ImageHandler::ImageHandler(QString filename)
{
    this->filename = filename;
//    convertImageToChar();
//    if(filename)
//    qDebug() <<
//    QImage image(filename);
//    QPixmap image(filename);
//    bool test = image.load(filename);   // true if successfully loaded
//    if(test){qDebug() << "[ImageHandler] image successfully loaded";}
//    else{qDebug() << "[ImageHandler] image loading failed";}
//    QPixmap pixmap;
//    QByteArray bytes;
//    QBuffer buffer(&bytes);
//    buffer.open(QIODevice::WriteOnly);
//    pixmap.save(&buffer, "PNG");
}

void ImageHandler::requestImage(){
    emit imreq();
}

QByteArray ImageHandler::convertImageToChar(){

//    QFile file;
//    file.setFileName(sFile);
//    if (!file.open(QIODevice::ReadOnly))
//    {
//        // some Error handling is done here
//    }
//    QByteArray bytearray = file.readAll();

    QFile file;
    file.setFileName(filename);
    QFileInfo tst(filename);
    file.open(QIODevice::ReadWrite);

    QByteArray pixarray = file.readAll();
    qDebug() << pixarray.mid(18,10);
//    qDebug() << tst.exists();
//    qDebug() << pixarray.capacity();
    if(pixarray.isEmpty()){
        qDebug() << "[ImageHandler] File is empty.";
        qDebug() << "[ImageHandler] File path is: " << tst.absolutePath();
    }else{
        qDebug() << "[ImageHandler] File path is: " << tst.absolutePath();
        qDebug() << "[ImageHandler] File size is: " << pixarray.size();
    }
    file.close();
    return pixarray;
    qDebug() << pixarray;
}

QString ImageHandler::getDataString(){
    QString imgpacket;
    imgpacket.append(convertImageToChar());
    return imgpacket;
}
